package com.qiaoba.module.job.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiaoba.module.job.entity.SysJobLog;
import com.qiaoba.module.job.entity.param.SysJobLogParam;
import com.qiaoba.module.job.mapper.SysJobLogMapper;
import com.qiaoba.module.job.service.SysJobLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时任务日志 服务层实现
 *
 * @author ailanyin
 * @date 2023-09-04 13:53:27
 */
@Service
@RequiredArgsConstructor
public class SysJobLogServiceImpl implements SysJobLogService {

    private final SysJobLogMapper sysJobLogMapper;

    @Override
    public int insert(SysJobLog sysJobLog) {
        return sysJobLogMapper.insert(sysJobLog);
    }

    @Override
    public List<SysJobLog> selectList(SysJobLogParam param) {
        return sysJobLogMapper.selectList(param2Wrapper(param));
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return sysJobLogMapper.deleteBatchIds(ids);
    }

    @Override
    public void cleanAll() {
        sysJobLogMapper.delete(null);
    }

    private Wrapper<SysJobLog> param2Wrapper(SysJobLogParam param) {
        LambdaQueryWrapper<SysJobLog> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .orderByDesc(SysJobLog::getStartTime)
                .between(ObjectUtil.isNotEmpty(param.getBeginTime()) && ObjectUtil.isNotEmpty(param.getEndTime()),
                        SysJobLog::getStartTime, param.getBeginTime(), param.getEndTime())
                .eq(StrUtil.isNotBlank(param.getStatus()), SysJobLog::getStatus, param.getStatus())
                .eq(StrUtil.isNotBlank(param.getJobName()), SysJobLog::getJobName, param.getJobName())
                .eq(StrUtil.isNotBlank(param.getJobGroup()), SysJobLog::getJobGroup, param.getJobGroup());
        return wrapper;
    }
}
